//
//  UnAmountViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/20.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON
import MJRefresh

class UnAmountViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    
    var datas:[JSON] = [JSON]()
    
    var page:Int = 1
    
    
//    var r:JSON = JSON(["type":type,"status":self.state,"complete_time_begin":"","complete_time_end":""])
    
    var t:JSON = jsonFormater(data: ["type":"","status":"","complete_time_begin":"","complete_time_end":""])
    
    @objc func openMenu(_ sender: UIBarButtonItem) {
        
        let vc = self.parent as! KYDrawerController
        
        vc.setDrawerState(vc.drawerState == .opened ? .closed : .opened, animated: true)
    }
    
    @objc func tokenOutOfDate(_ noti:NSNotification){
        let b = noti.object
        self.t = jsonFormater(data: b as Any)
        self.tableView.mj_header.beginRefreshing()
        let vc = self.parent as! KYDrawerController
        vc.setDrawerState(vc.drawerState == .opened ? .closed : .opened, animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.tokenOutOfDate(_:)), name: NSNotification.Name.rightRef2, object: nil)

        
        tableView.register(R.nib.unAmountTableViewCell(), forCellReuseIdentifier: UnAmountTableViewCell.cellIdenti())
        tableView.rowHeight = 165
        
        let vc = self.parent as! KYDrawerController
        
        vc.navigationItem.title = "出入金记录"
        
        vc.drawerDirection = .right
        
        vc.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "fliter_icon"), style: .plain, target: self, action: #selector(self.openMenu(_:)));
        loadData()
        tableView.mj_header = MJRefreshNormalHeader{ [weak self] in
            self?.page = 1
            self?.loadData()
        }
        
        tableView.mj_footer = MJRefreshAutoFooter {[weak self] in
            self?.loadData()
            
        }
        // Do any additional setup after loading the view.
    }
    
    func loadData() -> Void {
        
        NetWorkRequest(.inoutlists(param: ["type":t["type"].stringValue,"status":t["status"].stringValue,"complete_time_begin":t["complete_time_begin"].stringValue,"complete_time_end":t["complete_time_end"].stringValue,"page":self.page])) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                let jd = jsonFormater(data: res["data"] as Any)
                if jd["items"].dictionaryValue.isEmpty == false {
                    if self.page <= 1 {
                        self.datas.removeAll()
                    }
                    let jd2 = jsonFormater(data: jd["items"].dictionaryObject as Any)
                    
                    jd2["data"].arrayValue.forEach({ (d) in
                        self.datas.append(d)
                    })                                         
                    self.tableView.reloadData()
                    self.page += 1
                    
                    self.tableView.mj_footer.isHidden = jd2["data"].arrayValue.count <= 0
                    
                    self.tableView.mj_header.endRefreshing()
                    self.tableView.mj_footer.endRefreshing()
                }
                
                
            }
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datas.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UnAmountTableViewCell.cellIdenti()) as! UnAmountTableViewCell
        cell.selectionStyle = .none
        let js = jsonFormater(data: self.datas[indexPath.row])
        cell.labelStatus.text = js["status_name"].stringValue
        
        cell.labelAmount.text = "\(js["amount_show"].stringValue) 元"
        cell.labelDate.text = js["order_at"].stringValue
        cell.labelJinStatus.text = js["type_name"].stringValue
        cell.labelCate.text = js["inout_at"].stringValue //出入金时间
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
